package structure.myHeap;

import java.util.Arrays;

public class MyHeap {
    public int[] elem;
    public int usedSize;

    public MyHeap(){
        this.elem = new int[10];
    }

    /**
     * 向下调整 大根堆
     * @param parent 每棵树的根节点
     * @param len 每棵树调整的结束位置
     */
    public void shiftDown(int parent,int len){
        int child = 2*parent + 1;
        //最起码 是有左孩子的,至少有一个孩子
        while(child < len){
            if(child + 1 < len && elem[child] < elem[child+1] ){
                child++;//保证当前左右孩子最大值的下标
            }
            if(elem[child] > elem[parent]) {
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                parent = child;
                child = parent*2+1;
            }else {
                break;
            }
        }
    }

    /**
     * 创建堆
     * @param array 传入的数组
     */
    public void createHeap(int[] array){
        for (int i = 0; i < array.length ; i++) {
            elem[i] = array[i];
            usedSize++;
        }
        for (int parent = (usedSize-1-1)/2; parent >= 0; parent--) {
            //调整
            shiftDown(parent,usedSize);
        }
        System.out.println(Arrays.toString(elem));
    }

    /**
     * 向上调整 大根堆
     * @param child 堆最后一个节点的下标值
     */
    public void shiftUp(int child){
        int parent = (child-1)/2;
        while(child > 0){
            if (elem[child] > elem[parent]){
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                child = parent;
                parent = (child-1)/2;
            }else {
                break;
            }
        }
    }

    /**
     * 入队列 (尾插法) (大根堆)
     * @param val 入队的值
     */
    public void offer(int val){
        if (isFull()){
            //扩容
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
        //注意传参,因为入堆放在最末尾,而我们要得到堆末尾的index,故要减一.
        shiftUp(usedSize-1);
        System.out.println(Arrays.toString(elem));
    }

    public boolean isFull(){
        return usedSize == elem.length;
    }

    public boolean isEmpty(){
        return usedSize == 0;
    }

    /**
     * 出队 大根堆
     * @return 出队的值
     */
    public int poll(){
        if (isEmpty()){
            throw new RuntimeException("优先级队列为空!");
        }
        int tmp = elem[0];
        elem[0] = elem[usedSize-1];
        elem[usedSize-1] = tmp;
        usedSize--;
        shiftDown(0,usedSize);
        return tmp;
    }

    /**
     * 获取队头元素
     * @return 队头元素
     */
    public int peek(){
        if (isEmpty()){
            throw new RuntimeException("优先级队列为空!");
        }
        return elem[0];
    }

    /**
     * 堆排序
     */
    public void heapSort(){
        int end = usedSize-1;
        while(end > 0){
            int tmp = elem[0];
            elem[0] = elem[end];
            elem[end] = tmp;
            shiftDown(0,end);
            end--;
        }
        System.out.println(Arrays.toString(elem));
    }



}
